package com.ah.hf.service.impl;

import com.ah.hf.common.CustomException;
import com.ah.hf.entity.Category;
import com.ah.hf.entity.Dish;
import com.ah.hf.entity.Setmeal;
import com.ah.hf.mapper.CategoryMapper;
import com.ah.hf.service.CategoryService;
import com.ah.hf.service.DishService;
import com.ah.hf.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId, id);
        //判断是否关联菜品
        int count = dishService.count(qw);
        //判断是否大于零
        if (count > 0){
            //已经关联菜品，抛处一个业务异常
            throw new CustomException("已经关联菜品,不可删除");
        }
        LambdaQueryWrapper<Setmeal> qw1  =  new LambdaQueryWrapper<>();
        //
        qw1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(qw1);
        //判断是否关联菜品
        if (count1 > 0){
            //已经关联套餐，抛处一个业务异常
            throw new CustomException("已经关联套餐,不可删除");
        }
        super.removeById(id);
    }
}
